﻿using System;
using QuanLyThongTinCoSo.Common;

namespace QuanLyThongTinCoSo.BaseForm
{
    public partial class frmDetail : frmObject
    {
        #region "Variables"

        public string _fromtype = string.Empty;
        public int _id;

        #endregion

        #region "Delegate"

        public delegate void LoadData();

        public event LoadData OnLoadData = null;

        protected virtual void FireEvent()
        {
            if (OnLoadData != null)
            {
                OnLoadData();
            }
        }

        #endregion

        #region Constructor

        public frmDetail()
        { 
            InitializeComponent();
            _fromtype = Constant.TypeForm.New;
            this.ControlBox = true;
        }

        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Thêm mới vào csdl
        /// </summary>
        protected virtual void Insert()
        {
        }

        /// <summary>
        /// Hàm sửa bản ghi trong vào csdl
        /// </summary>
        protected virtual void Edit()
        {
        }

        /// <summary>
        /// Lưu dữ liệu và đóng form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.Compare(Constant.TypeForm.New, _fromtype, StringComparison.Ordinal) == 0)
            {
                if (Validation())
                {
                    Insert();
                    FireEvent();
                    Close();
                }
            }
            else
            {
                if (Validation())
                {
                    Edit();
                    FireEvent();
                    Close();
                }
            }
        }

        #region CheckControl

        /// <summary>
        /// Hàm kiểm tra các control
        /// </summary>
        /// <returns></returns>
        protected virtual Boolean Validation()
        {
            return true;
        }

        #endregion
    }
}